package com.blrs.common.utils;

import com.blrs.common.utils.uuid.UUID;

import java.lang.reflect.Field;

import java.lang.reflect.Field;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Objects;

//统一注入
public class ReflectionUtils {
    public static void setCommonPrivateProperty(Object obj) throws NoSuchFieldException, IllegalAccessException {
        Class<?> clazz = obj.getClass();
        Field idField = null;
        Field enterpriseIdField = null;
        Field createId=null;
        Field belongTo=null;
        Field creationTime=null;

        try {
            idField = clazz.getDeclaredField("id");
            idField.setAccessible(true);
        } catch (NoSuchFieldException e) {
            // id field not found, do nothing
        }
        try {
            createId = clazz.getDeclaredField("createId");
            createId.setAccessible(true);
        } catch (NoSuchFieldException e) {
            // id field not found, do nothing
        }
        try {
            belongTo = clazz.getDeclaredField("belongTo");
            belongTo.setAccessible(true);
        } catch (NoSuchFieldException e) {
            // id field not found, do nothing
        }
        if (createId != null && createId.get(obj) == null) {
            createId.set(obj, SecurityUtils.getUserId());
        }

        try {
            enterpriseIdField = clazz.getDeclaredField("enterpriseId");
            enterpriseIdField.setAccessible(true);
        } catch (NoSuchFieldException e) {
            // enterpriseId field not found, do nothing
        }

        if (idField != null && idField.get(obj) == null) {
            idField.set(obj, UUID.randomUUID().toString().replaceAll("-", ""));
        }

        if (enterpriseIdField != null && enterpriseIdField.get(obj) == null) {
            enterpriseIdField.set(obj, UUID.randomUUID().toString().replaceAll("-", ""));
        }
        if (belongTo != null && belongTo.get(obj) == null) {
            belongTo.set(obj,SecurityUtils.getOrgId());
        }
        try {
            creationTime=clazz.getDeclaredField("creationTime");
            creationTime.setAccessible(true);
        }catch (NoSuchFieldException e){

        }
        if (creationTime!=null && creationTime.get(obj)== null){
            creationTime.set(obj,new Timestamp(new Date().getTime()));
        }


        Field operateIdField = clazz.getDeclaredField("operateId");
        Field departmentIdField = clazz.getDeclaredField("departmentId");
        Field organizationIdField = clazz.getDeclaredField("organizationId");
        Field organizationNameField = clazz.getDeclaredField("organizationName");
        Field operateTimeField = clazz.getDeclaredField("operateTime");




        operateIdField.setAccessible(true);
        departmentIdField.setAccessible(true);
        organizationIdField.setAccessible(true);
        organizationNameField.setAccessible(true);
        operateTimeField.setAccessible(true);



        operateIdField.set(obj, SecurityUtils.getUserId());
        departmentIdField.set(obj, SecurityUtils.getDeptId());
        organizationIdField.set(obj, SecurityUtils.getOrgId());
        organizationNameField.set(obj, SecurityUtils.getOrgName());
        operateTimeField.set(obj, new Timestamp(new Date().getTime()));
    }




}